package com.pig4cloud.pig.homecenter.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.homecenter.vo.SyncAreaVO;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.homecenter.entity.HassAreaRegistryEntity;
import com.pig4cloud.pig.homecenter.service.HassAreaRegistryService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 区域数据
 *
 * @author pig
 * @date 2024-12-20 10:27:36
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/hassAreaRegistry" )
@Tag(description = "hassAreaRegistry" , name = "区域数据管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HassAreaRegistryController {

    private final  HassAreaRegistryService hassAreaRegistryService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param hassAreaRegistry 区域数据
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('homecenter_hassAreaRegistry_view')" )
    public R getHassAreaRegistryPage(@ParameterObject Page page, @ParameterObject HassAreaRegistryEntity hassAreaRegistry) {
        LambdaQueryWrapper<HassAreaRegistryEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(hassAreaRegistry.getName()),HassAreaRegistryEntity::getName,hassAreaRegistry.getName());
		wrapper.eq(HassAreaRegistryEntity::getUserId, SecurityUtils.getUser().getId());
        return R.ok(hassAreaRegistryService.page(page, wrapper));
    }


    /**
     * 通过条件查询区域数据
     * @param hassAreaRegistry 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @PreAuthorize("@pms.hasPermission('homecenter_hassAreaRegistry_view')" )
    public R getDetails(@ParameterObject HassAreaRegistryEntity hassAreaRegistry) {
		hassAreaRegistry.setUserId(SecurityUtils.getUser().getId());
        return R.ok(hassAreaRegistryService.list(Wrappers.query(hassAreaRegistry)));
    }

    /**
     * 新增区域数据
     * @param hassAreaRegistry 区域数据
     * @return R
     */
    @Operation(summary = "新增区域数据" , description = "新增区域数据" )
    @SysLog("新增区域数据" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('homecenter_hassAreaRegistry_add')" )
    public R save(@RequestBody HassAreaRegistryEntity hassAreaRegistry) {
		hassAreaRegistry.setUserId(SecurityUtils.getUser().getId());
        return R.ok(hassAreaRegistryService.save(hassAreaRegistry));
    }

    /**
     * 修改区域数据
     * @param hassAreaRegistry 区域数据
     * @return R
     */
    @Operation(summary = "修改区域数据" , description = "修改区域数据" )
    @SysLog("修改区域数据" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('homecenter_hassAreaRegistry_edit')" )
    public R updateById(@RequestBody HassAreaRegistryEntity hassAreaRegistry) {
        return R.ok(hassAreaRegistryService.updateById(hassAreaRegistry));
    }

    /**
     * 通过id删除区域数据
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除区域数据" , description = "通过id删除区域数据" )
    @SysLog("通过id删除区域数据" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('homecenter_hassAreaRegistry_del')" )
    public R removeById(@RequestBody Long[] ids) {

        return R.ok(hassAreaRegistryService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param hassAreaRegistry 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('homecenter_hassAreaRegistry_export')" )
    public List<HassAreaRegistryEntity> exportExcel(HassAreaRegistryEntity hassAreaRegistry,Long[] ids) {
        return hassAreaRegistryService.list(Wrappers.lambdaQuery(hassAreaRegistry).in(ArrayUtil.isNotEmpty(ids), HassAreaRegistryEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param hassAreaRegistryList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('homecenter_hassAreaRegistry_export')" )
    public R importExcel(@RequestExcel List<HassAreaRegistryEntity> hassAreaRegistryList, BindingResult bindingResult) {
        return R.ok(hassAreaRegistryService.saveBatch(hassAreaRegistryList));
    }

	@Operation(summary = "同步区域信息", description = "接收hass区域信息")
	@PostMapping("/pushAreas")
	public R pushAreas(@RequestBody SyncAreaVO vo) {
		if(vo.getSuccess()){
			hassAreaRegistryService.syncHassAreaRegistry(vo.getResult());
		}else{
			log.info(JSON.toJSONString(vo));
		}
		return R.ok();
	}

	/**
	 * 用户区域查询筛选
	 * @return
	 */
	@Operation(summary = "用户区域查询筛选" , description = "用户区域查询筛选" )
	@GetMapping("/select" )
	public R getHassAreaRegistrySelect() {
		LambdaQueryWrapper<HassAreaRegistryEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(HassAreaRegistryEntity::getUserId, SecurityUtils.getUser().getId());
		return R.ok(hassAreaRegistryService.list(wrapper));
	}
}
